package ss.pku.meeting.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ss.pku.meeting.entities.ApplicationRecord;
import ss.pku.meeting.entities.MeetingRecord;
import ss.pku.meeting.entities.Message;
import ss.pku.meeting.entities.Msg;
import ss.pku.meeting.mapper.MessageMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MessageService {

    @Autowired
    MessageMapper messageMapper;

    public int getUnreadCount(int deptId) {
        return messageMapper.selectUnreadCount(deptId);
    }

    public Msg select(Map<String, Object> para) {
        Integer status = null;
        if (para.containsKey("status")) {
            status = Integer.parseInt((String) para.get("status"));
        }
        Integer deptId = null;
        deptId = (Integer) para.get("deptId");
        Integer pageSize = Integer.parseInt((String) para.get("pageSize"));
        Integer currentPage = Integer.parseInt((String) para.get("currentPage"));
        Message message = new Message();
        message.setToId(deptId);
        message.setStatus(status);
        PageHelper.startPage(currentPage, pageSize);
        List<Message> messages = messageMapper.selectByCondition(message);
        PageInfo info = new PageInfo(messages);
        Map<String, Object> map = new HashMap<>();
        map.put("result", info);
        return Msg.success().add(map);
    }

    public Msg update(int id) {
        int size = messageMapper.update(id);
        return size>0?Msg.success():Msg.fault();
    }

    public void batchAdd(List<Integer> toIds, MeetingRecord meetingRecord) {
        String content = "您有一个新的会议提醒，会议地点为" + meetingRecord.getRoomNo() + "，会议日期为" + meetingRecord.getMeetingDate()+"，会议时间为" + meetingRecord.getMeetingTime() + "，会议主题为" + meetingRecord.getMeetingTheme();
        for (int toId : toIds) {
            messageMapper.insert(meetingRecord.getDeptId(), toId, content);
        }
    }
}
